package com.casic.config;

import com.casic.common.utils.Constant;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by huguoju on 2016/12/28.
 * 配置mybatis
 */
@Configuration
public class MybatisConfiguration {
    @Resource(name = "dataSource")
    private DataSource dataSource;

    @Value("${spring.datasource.driverClassName}")
    private String  driverClassName;

    //配置mybatis的分页插件pageHelper
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum","true");
        properties.setProperty("rowBoundsWithCount","true");
        properties.setProperty("reasonable","true");
        if(Constant.ORACLE_DRIVERCLASSNAME.equals(driverClassName)){
            //配置oracle数据库的方言
            properties.setProperty("dialect","oracle");
        }else if(Constant.MYSQL_DRIVERCLASSNAME.equals(driverClassName)){
            //配置mysql数据库的方言
            properties.setProperty("dialect","mysql");
        }else{
            properties.setProperty("dialect","oracle");
        }
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}
